﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RFIDStockManagement.Common.Public.Enums;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.IDAL;

namespace RFIDStockManagement.BLL
{
    public class StockCheckManager
    {
        private IStockCheck _db;

        public StockCheckManager()
        {
            _db = DAL.DalFactory.CreateStockCheckAcess();
        }

        /// <summary>
        /// 添加一个盘点周期信息
        /// </summary>
        /// <param name="stockId"></param>
        /// <param name="caption"></param>
        /// <param name="note"></param>
        /// <returns></returns>
        public int AddNewCheckSegment(string caption,string note)
        {
            StockCheckInfo newInfo = new StockCheckInfo();
            newInfo.FCaption = caption;
            newInfo.FNote = note;
            newInfo.FStartTime = DateTime.Now;
            newInfo.Status = EnumStockCheckStatus.Started;

            return _db.SaveStockCheckHeadInfo(newInfo);

        }

        /// <summary>
        /// 获取不同状态下的最后一个盘点周期
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public StockCheckInfo GetLastCheckInfo(EnumStockCheckStatus status)
        {
            return _db.GetStockCheckMainInfo((int)status);
        }


        /// <summary>
        /// 获取不同状态下的盘点周期列表
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<StockCheckInfo> GetList(EnumStockCheckStatus status)
        {
            return _db.GetStockCheckList((int)status);
        }


        /// <summary>
        /// 获取特定的盘点周期信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public StockCheckInfo GetStockCheckInfo(int id)
        {
            return _db.GetStockCheckMainInfoById(id);
        }

        /// <summary>
        /// 更改当前盘点信息的状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public bool SetStatus(int id, EnumStockCheckStatus status)
        {
            return _db.UpdateStatus(id, (int)status);
        }

        /// <summary>
        /// 添加盘点明细
        /// </summary>
        /// <param name="fid">当前盘点周期</param>
        /// <param name="infos"></param>
        /// <returns></returns>
        public bool SaveCheckDetails(int fid, List<StockCheckDetail> infos)
        {
            return _db.SaveItemDetailInfo(fid, infos);
        }


        public bool Delete(int fid,out string errStr)
        {
            return _db.Delete(fid, out errStr);
        }
    }
}
